import Login from './views/public/Login.vue'
import NotFound from './views/public/404.vue'
import Unauthorized from './views/public/401.vue'
import Home from './views/public/Home.vue'
import service_provider from './views/public/nav3/service_provider.vue'
import set_update from './views/public/nav3/set_update.vue'
import section from './views/teacher/section.vue'
import section_detail from './views/teacher/section_detail.vue'
import major from './views/manager/public/major.vue'
import permession from './views/super/permession.vue'
import role from './views/super/role.vue'
import courseInform from './views/manager/public/courseInform.vue'
import plan from './views/manager/public/plan.vue'
//引入教师管理页面--冯云威
import teachers_manager from './views/teacher/teachersManager.vue'
//引入添加教师页面 --冯云威
import add_teachers from './views/teacher/addTeacher.vue'
//引入成绩管理页面 --冯云威
import score from './views/teacher/score.vue'
//学费管理页面 - 周长磊
import tuition from './views/manager/Tuition.vue'
//教务主页 侯雪茹 8.8
import education from './views/manager/edu.vue'
import student from './views/manager/student.vue'//学生管理
import thesis from './views/manager/public/ManageThesis.vue'    //论文管理
import thesisLayout from './views/manager/public/thesisLayout.vue'    //论文规范
import courseManage from './views/manager/public/courseManage.vue'     //课程管理
import enroll from './views/manager/enroll.vue'
import selectSubject from './views/manager/selectSubject.vue'
import judgeSubject from './views/manager/judgeSubject.vue'
import exammanger from './views/teacher/exammanger.vue'

//2017/8/8 by 雷紫辉  新增视频答疑页面
import videoQA from './views/teacher/videoQA.vue'

import Vue from 'vue'
import VueRouter from 'vue-router'


//张文东  审核
import my_311 from './views/recruit/public/operation.vue'
import my_322 from './views/recruit/public/pay.vue'
import my_333 from './views/recruit/public/score.vue'
import my_344 from './views/recruit/public/record.vue'
import shi_21 from './views/recruit/public/shi_21.vue'

//申忠正 资源 课件
import teacher_resource from './views/teacher/resource.vue'
import teacher_courseware from './views/teacher/courseware.vue'

//辛亚会 异动管理
import tchange from './views/teacher/tchange.vue'

// 张建磊 公告管理
import notice from './views/manager/public/Notice.vue'

Vue.use(VueRouter);

let routes = [{
    path: '/login',
    component: Login,
    name: '登录',
    hidden: true,
    meta: {
        requiresAuth: 0
    }
},
    {
        path: '/404',
        component: NotFound,
        name: '404',
        hidden: true,
        meta: {
            requiresAuth: 0
        }
    },
    {
        path: '/401',
        component:Unauthorized ,
        name: '401',
        hidden: true,
        meta: {
            requiresAuth: 0
        }
    },
    {
        path: '/home',
        component:  Home,
        name: '主页',
        hidden: true,
        meta: {
            requiresAuth: 0
        }
    },
    {
        path: '/quan',
        component: Home,
        hidden: false,
        name: '权限模块',
        iconCls: 'el-icon-star-on',
        leaf: true,
        children: [
            {path: '/quaxian', component: permession, name: '权限管理'},
        ],
        meta: {
            requiresAuth: 1
        }

    },
    {
        path: '/role',
        component: Home,
        hidden: false,
        name: '角色模块',
        iconCls: 'el-icon-star-on',
        leaf: true,
        children: [
            {path: '/roleManage', component: role, name: '角色管理'},
        ],
        meta: {
            requiresAuth: 1
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        name: '教务主页',
        hidden: false,
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/jiao_11', component: education,name: '教务主页'},
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        hidden: false,
        name: '学生管理',
        iconCls: 'el-icon-document',
        leaf: true,
        children: [
            {path: '/jiao_12', component: student, name: '学生管理'},
        ],
        meta: {
            requiresAuth: 2

        }

    },

    {
        path: '/jiaowu',
        component: Home,
        name: '学费管理',
        hidden: false,
        iconCls: 'el-icon-star-on',
        leaf: true,
        children: [
            // { path: '/jiao_13', name: '学费管理' },
            {path: '/tuition', component: tuition, name: '学费管理'}
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        hidden: false,
        name: '教学管理',
        iconCls: 'el-icon-edit',
        leaf: true,
        children: [
            {path: '/jiao_14', name: '教学管理'},
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        name: '教师管理',
        hidden: false,
        iconCls: 'el-icon-star-off',
        leaf: true,
        children: [
            {path: '/teachers_manager', component: teachers_manager, name: '教师管理'},
            {path: '/add_teachers', component: add_teachers, name: '添加教师'}
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        hidden: false,
        name: '专业管理',
        iconCls: 'el-icon-star-on',
        leaf: true,
        children: [
            // { path: '/jiao_16', name: '专业管理' },
            {path: '/major', component: major, name: '专业管理'}
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        hidden: false,
        name: '课程管理',
        iconCls: 'el-icon-star-on',
        leaf: true,
        children: [
            {path: '/jiao_17', component: courseManage, name: '课程管理'},
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        hidden: false,
        name: '招生计划',
        iconCls: 'el-icon-star-on',
        leaf: true,
        children: [
            {path: '/jiao_18', component: enroll, name: '招生计划'},
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        name: '异动审核',
        hidden: false,
        iconCls: 'el-icon-star-on',
        leaf: true,
        children: [
            {path: '/tchange', component: tchange, name: '异动审核'},
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        name: '公告管理',
        hidden: false,
        iconCls: 'el-icon-date',
        leaf: true,
        children: [
            // { path: '/jiao_20', name: '公告' },
            {path: '/notice', component: notice, name: '公告管理'}
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        name: '论文管理',
        hidden: false,
        iconCls: 'el-icon-document',
        leaf: true,
        children: [
            {path: '/jiao_21', component: thesis, name: '论文管理'},
            {path: '/layout', component: thesisLayout, name: '论文标准'}
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        hidden: false,
        name: '教学计划',
        iconCls: 'el-icon-document',
        leaf: true,
        children: [
            {path: '/plan', component: plan, name: '教学计划'},
        ],
        meta: {
            requiresAuth: 2
        }

    },
    {
        path: '/jisohi',
        component: Home,
        hidden: false,
        name: '教师主页',
        iconCls: 'el-icon-message', //文字旁边的小图标
        leaf: true,
        /*
        * 2017/8/3 王娇
        * 添加一个教师主页
        * 展示公告信息
        * */
        children: [
            {path: '/shi_21',component:shi_21, name: '教师主页'},
        ],
        meta: {
            requiresAuth: 3
        }
    },

    {
        path: '/point',
        component: Home,
        name: '成绩管理',
        hidden: false,
        iconCls: 'fa fa-bar-chart',
        leaf: true,
        children: [
            {path: '/shi_22', name: '成绩管理', component: score},
        ],
        meta: {
            requiresAuth: 3
        }
    },
    {
        path: '/',
        component: Home,
        name: '考试管理',
        iconCls: 'el-icon-edit',
        children: [
            {path: '/selectSubject', component: selectSubject, name: '选择题录入'},
            {path: '/judgeSubject', component: judgeSubject, name: '判断题录入'}

        ],
        hidden: false,
        meta: {
            requiresAuth: 3
        }
    },
    {
        path: '/',
        hidden: false,
        component: Home,
        name: '课程通知',
        iconCls: 'el-icon-time',
        leaf: true,
        children: [
            {path: '/courseInform', component: courseInform, name: '课程通知'},
        ],
        meta: {
            requiresAuth: 3
        }

    },
    {
        path: '/',
        component: Home,
        name: '考试信息',
        hidden: false,
        iconCls: 'el-icon-document',
        leaf: true,
        children: [
            {path: '/exammanger', name: '考试管理', component: exammanger},
        ],
        meta: {
            requiresAuth: 3
        }
    },
    {
        path: '/',
        component: Home,
        name: '网上答疑',
        iconCls: 'el-icon-edit',
        children: [//2017/8/8 by 雷紫辉 修改路由指向，废弃原先页面
            {path: '/videoQA', name: '视频答疑', component: videoQA},
            {path: '/my_2', name: '论坛答疑'}

        ],
        hidden: false,
        meta: {
            requiresAuth: 3
        }
    },
    {
        path: '/',
        component: Home,
        name: '课程资源管理',
        hidden: false,
        iconCls: 'fa fa-id-card-o',
        children: [

            {path: '/teacher_resource', component: teacher_resource, name: '导学资源'},
            {path: '/teacher_courseware/:id', component: teacher_courseware, name: '导学课件', hidden: true},
            {path: '/section_detail', name: '管理课程章节', component: section_detail},
            {path: '/store_query', name: '题库'},
        ],
        meta: {
            requiresAuth: 3
        }
    },

    {
        path: '/',
        component: Home,
        name: '审核人员',
        hidden: false,
        iconCls: 'el-icon-information',
        leaf: true,
        children: [
            {path: '/my_311', component: my_311, name: '审核人员'},
        ],
        meta: {
            requiresAuth: 4
        }

    },

   

    {
        path: '/shehe1',
        component: Home,
        name: '缴费信息',
        hidden: false,
        leaf: true,
        iconCls: 'el-icon-plus',
        children: [
            {path: '/my_322', component: my_322, name: '缴费详情'},
        ],
        meta: {
            requiresAuth: 4
        }

    },

    {
        path: '/jiaowu',
        component: Home,
        name: '入学成绩',
        hidden: false,
        iconCls: 'el-icon-document',
        leaf: true,
        children: [
            {path: '/my_333', component: my_333, name: '入学成绩'},
        ],
        meta: {
            requiresAuth: 4
        }

    },
    {
        path: '/jiaowu',
        component: Home,
        name: '历史记录',
        hidden: false,
        iconCls: 'el-icon-d-arrow-left',
        leaf: true,
        children: [
            {path: '/my_344', component: my_344, name: '历史记录'},
        ],
        meta: {
            requiresAuth: 4
        }

    },
    {
        path: '*',
        hidden: true,
        redirect: {path: '/404'},
        meta: {
            requiresAuth: 0
        }
    }
];

const router = new VueRouter({
    routes,
    mode:'history'
});

const ignore=["/login"];//不需要登陆权限的路由

router.beforeEach((to, from, next) => {
    let user = JSON.parse(sessionStorage.getItem('user'));
    if (!user){//非登陆用户
        if(ignore.indexOf(to.path)<0){//不在忽略列表里边
            next({path:"/login"})
        }
        else {
            next();
        }
    }
    else{//登陆的用户
        if (to.path=="/home"){// 自动跳相应的主页，而不是空白
            switch (user.roleId+1){
                case 1:// 超管
                    next({path:"/quaxian"});break;
                case 2://教务人员
                    next({path:"/jiao_11"});break;
                case 3://普通教师
                    next({path:"/shi_21"});break;
                case 4://审核人员
                    next({path:"//my_311"});break;
                default:next();
            }
        }
        else if(to.path=="/"){//浏览器直接输入网址，就移除登陆状态，返回到登陆页面
            sessionStorage.removeItem("user");
            next({path:"/login"})
        }
        else if(ignore.indexOf(to.path)==-1){//当前登陆用户访问非忽略页面
            router == filter(routes, (user.roleId - 0) + 1);
            next();
        }
    }

    // if (to.path == '/login') {
    //     if (user != null) {
    //         if (user.rememberMe == true) {
    //             next({ path: '/home' })
    //         }
    //     }
    // }
    // if (!user && to.path != '/login') {
    //     next({ path: '/login' })
    // } else {
    //     if (to.path != '/login') {
    //         router == filter(routes, (user.roleId - 0) + 1);
    //         next();
    //     }
    // }
    // next();
});
//循环过滤路由
function filter(filerounte, role) {
    for (var index in filerounte) {
        var myrouterole = filerounte[index].meta.requiresAuth
        if (myrouterole != role && myrouterole != 0) {
            filerounte[index].hidden = true;
        } else if (myrouterole == role) {
            filerounte[index].hidden = false;
        }
    }
    return filerounte;
}

export default router;